package cn.valorin.event;

import static cn.valorin.configuration.languagefile.MessageSender.sm;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import cn.valorin.gui.MyInventoryHolder;
import cn.valorin.item.Bean;
import cn.valorin.item.RawZongzi;
import cn.valorin.item.Rice;
import cn.valorin.util.CheckLore;
import cn.valorin.via.Via;

public class Gui1Event implements Listener {
	@EventHandler
	public void click(InventoryClickEvent e) {
		Inventory inv = e.getInventory();
		if (inv == null) { return; }
		if (inv.getHolder() == null) { return; }
		if (!(inv.getHolder() instanceof MyInventoryHolder)) { return; }
		if (((MyInventoryHolder)inv.getHolder()).getType() != 1) { return; }
		int slot = e.getRawSlot();
		if (e.getClick().equals(ClickType.DOUBLE_CLICK)) {
			if (slot > 53 && slot < 90) {
			    e.setCancelled(true);
			    return;
			}
		}
		int[] availableSlots = {11,12,13,14,15,20,21,22,23,24,29,30,31,32,33,38,39,40,41,42,35};
		if (slot >=0 && slot <= 53) {
			List<Integer> availableSlotsList = new ArrayList<Integer>();
		    for (int s : availableSlots) {
		    	availableSlotsList.add(s);
		    }
			if (!availableSlotsList.contains(slot)) { 
				e.setCancelled(true);
			}
		}
		
		Player opener = (Player) e.getWhoClicked();
		if (slot == 26) {
			if (inv.getItem(35)!=null && !CheckLore.check(inv.getItem(35), RawZongzi.get(opener))) {
				sm("&c制作失败——有东西阻挡在成品槽",opener);
				return;
			}
			if (inv.getItem(35)!=null && inv.getItem(35).getAmount() == 64) {
				sm("&c制作失败——请取走粽子，否则无法继续合成",opener);
				return;
			}
			int[] riceSlots = {11,12,13,14,15,20,24,29,33,38,39,40,41,42};
			for (int riceSlot : riceSlots) {
				if (!CheckLore.check(inv.getItem(riceSlot), Rice.get(opener))) {
					sm("&c制作失败——糯米未放置完全",opener);
					return;
				}
			}
			int[] beanSlots = {30,31,32};
			for (int beanSlot : beanSlots) {
				if (!CheckLore.check(inv.getItem(beanSlot), Bean.get(opener))) {
					sm("&c制作失败——豆未放置完全",opener);
					return;
				}
			}
			int[] fillingsSlots = {21,22,23};
			for (int fillingsSlot : fillingsSlots) {
				if (inv.getItem(fillingsSlot) == null) {
					sm("&c制作失败——馅料未放置完全",opener);
					return;
				}
				if (!inv.getItem(fillingsSlot).getType().equals(Material.COOKED_BEEF) &&
					!inv.getItem(fillingsSlot).getType().equals(Material.COOKED_CHICKEN) &&
					!inv.getItem(fillingsSlot).getType().equals(Via.cooked_fish) &&
					!inv.getItem(fillingsSlot).getType().equals(Material.COOKED_MUTTON) &&
					!inv.getItem(fillingsSlot).getType().equals(Material.COOKED_RABBIT) &&
					!inv.getItem(fillingsSlot).getType().equals(Via.grilled_pork)) {
					sm("&c制作失败——馅料未放置完全",opener);
					return;
				}
			}
			
			int[] materialSlots = {11,12,13,14,15,20,21,22,23,24,29,30,31,32,33,38,39,40,41,42};
			for (int materialSlot : materialSlots) {
				if (inv.getItem(materialSlot).getAmount() == 1) {
					inv.setItem(materialSlot, new ItemStack(Material.AIR));
				} else {
					ItemStack temp = inv.getItem(materialSlot);
					temp.setAmount(temp.getAmount() - 1);
					inv.setItem(materialSlot, temp);
				}
			}
			if (inv.getItem(35) == null) {
				inv.setItem(35, RawZongzi.get(opener));
			} else {
				ItemStack temp = inv.getItem(35);
				temp.setAmount(temp.getAmount() + 1);
				inv.setItem(35, temp);
			}
		}
	}
	@EventHandler
	public void close(InventoryCloseEvent e) {
		Inventory inv = e.getInventory();
		if (inv == null) { return; }
		if (inv.getHolder() == null) { return; }
		if (!(inv.getHolder() instanceof MyInventoryHolder)) { return; }
		if (((MyInventoryHolder)inv.getHolder()).getType() != 1) { return; }
		Player player = (Player) e.getPlayer();
		int[] slots = {11,12,13,14,15,20,21,22,23,24,29,30,31,32,33,38,39,40,41,42,35};
		Location loc = player.getLocation();
		for (int slot : slots) {
			if (inv.getItem(slot) != null) {
			    player.getWorld().dropItem(loc, inv.getItem(slot));
			}
		}
	}
}
